<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
     <el-form-item label="品牌名称:">
        <el-input v-model="queryParams.brandName" size="small" placeholder="请输入品牌名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="分类名称:">
        <el-input v-model="queryParams.categoryName" size="small" placeholder="请输入分类名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
     <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['pms:sku:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['pms:sku:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['pms:sku:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['pms:sku:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleLogicalDelete">作废</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="工具名称" align="center" prop="skuName" />

      <el-table-column label="品牌名称" align="center" prop="brandName" />
    <!--  <el-table-column label="分类ID" align="center" prop="categoryId" /> -->
      <el-table-column label="分类名称" align="center" prop="categoryName" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="规格" align="center" prop="spec" />
    <!--  <el-table-column label="库存数量" align="center" prop="qty" />
      <el-table-column label="库存预警数量" align="center" prop="alertQty" />
      <el-table-column label="销量" align="center" prop="saleQty" />
      <el-table-column label="评论数" align="center" prop="commentQty" /> -->
      <!-- <el-table-column label="商品状态" align="center" prop="status" /> -->
      <el-table-column label="商品条码" align="center" prop="sn" />
   <!--   <el-table-column label="品牌ID" align="center" prop="brandId" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['pms:sku:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['pms:sku:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="skuFormDialog.title" :visible.sync="skuFormDialog.visible" :close-on-click-modal="false" :before-close="resetSkuFormDialog" width="500px" append-to-body>
      <el-form ref="skuForm" :model="skuFormData" :rules="skuFormRules" label-width="80px">
        <!-- <el-form-item label="SPU_ID" prop="spuId">
          <el-input v-model="skuFormData.spuId" placeholder="请输入SPU_ID" />
        </el-form-item> -->
        <el-form-item label="工具名称" prop="skuName">
          <el-input v-model="skuFormData.skuName" placeholder="请输入工具名称" />
        </el-form-item>

     <!--   <el-form-item label="品牌ID" prop="brandId">
          <el-input v-model="skuFormData.brandId" placeholder="请输入品牌ID" />
        </el-form-item> -->
        <el-form-item label="品牌名称" prop="brandName">
          <el-input v-model="skuFormData.brandName" placeholder="请输入品牌名称" />
        </el-form-item>
        <!-- <el-form-item label="分类ID" prop="categoryId">
          <el-input v-model="skuFormData.categoryId" placeholder="请输入分类ID" />
        </el-form-item>
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="skuFormData.categoryName" placeholder="请输入分类名称" />
        </el-form-item> -->
       <!-- <el-form-item label="价格" prop="price">
          <el-input v-model="skuFormData.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="skuFormData.weight" placeholder="请输入重量" />
        </el-form-item> -->
        <el-form-item label="规格" prop="spec">
          <el-input v-model="skuFormData.spec" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="工具条码" prop="sn">
          <el-input v-model="skuFormData.sn" placeholder="请输入工具条码" />
        </el-form-item>
    <!--    <el-form-item label="库存数量" prop="qty">
          <el-input v-model="skuFormData.qty" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="库存预警数量" prop="alertQty">
          <el-input v-model="skuFormData.alertQty" placeholder="请输入库存预警数量" />
        </el-form-item>
        <el-form-item label="商品图片" prop="image">
          <el-input v-model="skuFormData.image" placeholder="请输入商品图片" />
        </el-form-item>
        <el-form-item label="商品图片列表" prop="images">
          <el-input v-model="skuFormData.images" placeholder="请输入商品图片列表" />
        </el-form-item>
        <el-form-item label="销量" prop="saleQty">
          <el-input v-model="skuFormData.saleQty" placeholder="请输入销量" />
        </el-form-item>
        <el-form-item label="评论数" prop="commentQty">
          <el-input v-model="skuFormData.commentQty" placeholder="请输入评论数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="skuFormData.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSkuFormDialog">确 定</el-button>
        <el-button @click="resetSkuFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import SkuApi from '@/api/pms/SkuApi'

const defaultSkuFormData = {
  spuId: null,
  skuName: null,
  sn: null,
  brandId: null,
  brandName: null,
  categoryId: null,
  categoryName: null,
  price: null,
  weight: null,
  spec: null,
  qty: null,
  alertQty: null,
  image: null,
  images: null,
  saleQty: null,
  commentQty: null,
  status: '0'
}
export default {
  name: 'SkuList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        spuId: null,
        skuName: null,
        sn: null,
        brandId: null,
        brandName: null,
        categoryId: null,
        categoryName: null,
        price: null,
        weight: null,
        spec: null,
        qty: null,
        alertQty: null,
        image: null,
        images: null,
        saleQty: null,
        commentQty: null,
        status: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      skuFormDialog: {
        title: '',
        visible: false
      },
      skuFormData: Object.assign({}, defaultSkuFormData),
      skuFormRules: {
        skuName: [{ required: true, message: 'SKU名称不能为空', trigger: 'blur' }],
        sn: [{ required: true, message: '商品条码不能为空', trigger: 'blur' }],
        price: [{ required: true, message: '价格不能为空', trigger: 'blur' }],
        qty: [{ required: true, message: '库存数量不能为空', trigger: 'blur' }]
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openSkuFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SkuApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleLogicalDelete(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认作废数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return BrandApi.logicalDelete(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('作废成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openSkuFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      SkuApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SkuApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openSkuFormDialog(id) {
      this.skuFormDialog.visible = true
      if (id) {
        this.skuFormDialog.title = '随车工具登记'
        SkuApi.get(id).then(response => {
          this.skuFormData = {
            ...this.skuFormData,
            ...response.data
          }
        })
      } else {
        this.skuFormDialog.title = '随车工具登记'
      }
    },
    confirmSkuFormDialog() {
      this.$refs['skuForm'].validate(valid => {
        if (valid) {
          if (this.skuFormData.id) {
            SkuApi.edit(this.skuFormData).then(response => {
              this.$message.success('修改成功')
              this.resetSkuFormDialog()
              this.handleQuery()
            })
          } else {
            SkuApi.add(this.skuFormData).then(response => {
              this.$message.success('添加成功')
              this.resetSkuFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetSkuFormDialog() {
      this.skuFormDialog = {
        title: '',
        visible: false
      }
      this.skuFormData = Object.assign({}, defaultSkuFormData)
      this.resetForm('skuForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
